package com.example.publishDemo.Controller;


import com.example.publishDemo.Model.AjaxResult;
import com.example.publishDemo.Model.Article;
import com.example.publishDemo.Model.User;
import com.example.publishDemo.Model.UserRelation;
import com.example.publishDemo.Service.ArticleService;
import com.example.publishDemo.Service.UserService;
import com.example.publishDemo.Tools.Tool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//@CrossOrigin
@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping
public class UserController {

    @Resource
    UserService service;

    @Resource
    ArticleService articleService;

//    @PostMapping("/zz/ztc")
//    public boolean regUserztc(@RequestBody User user) {
//        System.out.println(user.getUsername());
//        int i = service.addUser(user.getUsername(),user.getPassword(),user.getGender());
//        if (i != -1) {
//            return true;
//        }
//        return false;
//    }

    @GetMapping("/isexist")
    public boolean findUser(@RequestParam String strname) {
        boolean find = false;
        String username = strname;
        List<User> users = service.checkName(username);
        if (users.size() > 0) {
            find = true;
        }
        return find;
    }

    @GetMapping("/isexistExcludingMe")
    public boolean findUser(@RequestParam String strname, int u_id) {
        boolean find = false;
        String username = strname;
        List<User> users = service.checkNameExcludingMe(username,u_id);
        if (users.size() > 0) {
            find = true;
        }
        return find;
    }

    @PostMapping("/regist")
    public boolean a(@RequestBody User user) {
        user.setBirthday(LocalDate.now());
        int i = -1;
        try {
            i = service.registe(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (i != -1) {
            return true;
        }
        return false;
    }

    @PostMapping("/login")
    public User login(@RequestBody User user) {
        return service.login(user);
    }

    @PostMapping("/userlist")
    public List<User> getUserlist() {
        return service.userlist();
    }

    @GetMapping("/deluser")
    public Boolean delUser(@RequestParam int id) {
        int i = -1;
        try {
            i = service.delUser(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (i != -1) {
            return true;
        }
        return false;
    }

    @GetMapping("/user/buildRelationship")
    public AjaxResult buildRelationship(@RequestParam int focus_id, int focused_id) {
        List<UserRelation> relation = service.isRelation(focus_id, focused_id);
        if (!relation.isEmpty() && relation.size() > 0) {
            service.UnFollow(focus_id, focused_id);
            return AjaxResult.error("已取消关注", false);
        }
        int i = service.buildRelationship(focus_id, focused_id);
        if (i > 0) {
            return AjaxResult.success("关注成功", true);
        }
        return AjaxResult.error("err", false);
    }

//    @GetMapping("/user/buildRelationship")
//    public AjaxResult buildRelationship(@RequestParam int focus_id, int focused_id) {
//        List<UserRelation> relation = service.isRelation(focus_id, focused_id);
//        if (!relation.isEmpty() && relation.size() > 0) {
//            return AjaxResult.error("已关注", false);
//        }
//        int i = service.buildRelationship(focus_id, focused_id);
//        if (i > 0) {
//            return AjaxResult.success("关注成功", true);
//        }
//        return AjaxResult.error("err", false);
//    }

    @GetMapping("/user/isRelation")
    public AjaxResult isRelation(@RequestParam int focus_id, int focused_id) {
        List<UserRelation> relation = service.isRelation(focus_id, focused_id);
        if (!relation.isEmpty() && relation.size() > 0) {
            return AjaxResult.success(true);
        }
        return AjaxResult.error("err", false);
    }

    @GetMapping("/user/getUserInfo")
    public AjaxResult getUserInfo(@RequestParam int u_id) {
        Map<String, Object> map = new HashMap<>();

        User user = service.getUserForId(u_id);
        int fans = service.getUserFansForId(u_id).size();
        int views = articleService.allViewsForId(u_id);
        int maxView = articleService.maxView(u_id);
        List<Article> allArticleForId = articleService.getAllArticleForId(u_id);

        map.put("user", user);
        map.put("fans", fans);
        map.put("views", views);
        map.put("maxView", maxView);
        map.put("articles", allArticleForId);


        return AjaxResult.success(map);
    }

    @GetMapping("/user/changeUserStatus")
    public AjaxResult changeUserStatus(@RequestParam int u_id,String userStatus) {
        service.changUserStatus(u_id, userStatus);
        return AjaxResult.success(true);
    }

    @GetMapping("/user/changeUserInfo")
    public AjaxResult changeUserInfo(@RequestParam String username, String password,int u_id) {
        service.changeUserInfo(username, password, u_id);
        return AjaxResult.success(true);
    }

    @GetMapping("/user/changeUserGravatar")
    public AjaxResult changeUserGravatar(@RequestParam String picture,int u_id) {
        service.changeUserGravatar(picture, u_id);
        return AjaxResult.success(true);
    }

    @GetMapping("/user/GetAllUsersOfFocusId")
    public AjaxResult GetAllUsersOfFocusId(@RequestParam int u_id,int start,int end) {
        List<Integer> users = service.GetAllUsersOfFocusId(u_id);
        List<User> collect = users.stream().map(userId -> service.getUserForId(userId)).collect(Collectors.toList());
        return AjaxResult.success(Tool.subList(collect,start,end));
    }

}
